﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    private int id;
    private Container container;
    private List<Location> lstLocations;
    private List<FoodCategory> lstFoodCategories;


    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["id"] == null)
        {
            Response.Redirect("default.aspx");
        }
        
        id = int.Parse(Request.QueryString["id"]);

        

        using(CCSEntities db = new CCSEntities())
        {
            container = (from c in db.Containers
                         where c.ContainerID == id
                         select c).FirstOrDefault();

            if (!Page.IsPostBack)
            {

                lstLocations = db.Locations.OrderBy(x => x.RoomName).ToList();
                ddlLocation.DataSource = lstLocations;
                ddlLocation.DataBind();

                lstFoodCategories= db.FoodCategories.OrderBy(x => x.CategoryType).ToList();
                ddlType.DataSource = lstFoodCategories;
                ddlType.DataBind();

                if (container != null)
                {

                    lblID.Text = container.BinNumber.ToString();
                    txtWeight.Text = container.Weight.ToString();
                    if (container.FoodCategory != null)
                        ddlType.Items.FindByValue(container.FoodCategoryID.ToString()).Selected = true;

                    if (container.Location != null)
                        ddlLocation.Items.FindByValue(container.LocationID.ToString()).Selected = true;

                }
            }
        }

    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        double num;
        if (!(double.TryParse(txtWeight.Text, out num))) //checks that it is a number
        {
            lblWeightError.Text = "Invalid Weight Number!";   
        }
        else if ((double.Parse(txtWeight.Text)) <0)  //checks that number is not less than 0
        {
            lblWeightError.Text = "Invalid Weight Number!";
        }
        else 
        {
            double weight = double.Parse(txtWeight.Text);
            int location = int.Parse(ddlLocation.SelectedValue);
            int foodType = int.Parse(ddlType.SelectedValue);

            //container.Weight = weight;
            container.LocationID = (short)location;
            container.FoodCategoryID = (short)foodType;

            Session["Container"] = container;

            Response.Redirect("confirm.aspx");
            //Response.Redirect("confirm/?id="+id+"&weight="+weight+"&location="+location+"&type="+foodType);
            
        }
       
    }
}